<template>
	<view class="new-users copy-data">
		<!-- 筛选条件和搜索框 -->
		<view class="searchBox">
			<view class="filterBox">
				<picker @change="onFilterChange" :value="filterIndex" :range="filterOptions" class="filterPicker">
					<view class="filterText">{{ filterOptions[filterIndex] }}</view>
				</picker>
			</view>
			<uni-search-bar ref="searchBar" radius="30" placeholder="请输入关键字" clearButton="auto" cancelButton="none"
				@confirm="onSearchConfirm" class="searchInput" />
		</view>

		<!-- VIP 列表 -->
		<view class="moneyBox" v-for="(item, index) in vipList" :key="index">
			<view class="topUser">
				<image class="avatar" :src="item.avatar" mode=""></image>
				<view style="width: 40%;">
					<p>{{ item.nickname }}</p>
					<p>UID：{{ item.uid }}</p>
				</view>
				<view style="width: 40%;text-align: right;">
					等级：{{ item.yiXingDistributionLevel && item.yiXingDistributionLevel.level || '暂无' }}
				</view>
			</view>
			<view class="bottomMoney">
				<p>手机号：{{ item.phone }}</p>
				<p>当前余额：{{ item.nowMoney }}</p>
				<p>推荐人：{{ item.puser && item.puser.inviterMsqUser.nickname }}</p>
				<p>当前佣金：{{ item.commission }}</p>
			</view>
		</view>

		<!-- 加载提示 -->
		<view v-if="loading" class="loading">加载中...</view>
		<view v-if="noMore" class="no-more">没有更多了</view>
	</view>
</template>
<script>
	import {
		getUserInfo
	} from '@/api/user.js';
	import {
		getUserList
	} from '@/api/business.js';

	export default {
		data() {
			return {
				userInfo: {},
				vipList: [],
				pageSize: 10, // 每页显示条目数
				pageNum: 1, // 当前页码
				filterIndex: 0, // 筛选条件索引，默认为 nickname
				searchKeyword: null, // 搜索关键字
				filterOptions: ['按昵称', '按手机号', '按用户ID'], // 筛选条件选项
				loading: false,
				noMore: false
			};
		},
		onLoad() {
			this.getUserDetail();
		},
		methods: {
			getUserDetail() {
				getUserInfo().then((res) => {
					this.userInfo = res.data;
					this.loadMoreVips(this.userInfo.msqStore.id);
				});
			},
			async loadMoreVips(storeId) {
				if (this.loading) return;

				this.loading = true;
				const data = {
					storeId: storeId,
					pageSize: this.pageSize,
					pageNum: this.pageNum,
					nickname: this.filterIndex == 0 ? this.searchKeyword : null,
					phone: this.filterIndex == 1 ? this.searchKeyword : null,
					uid: this.filterIndex == 2 ? this.searchKeyword : null,
				};

				try {
					const res = await getUserList(data);
					const newList = res.data.records || []; // 假设返回的数据结构有一个records字段
					// 追加到现有列表
					this.vipList = this.vipList.concat(newList);

					// 更新当前页码
					this.pageNum++;

					// 如果新获取的数据长度小于每页大小，则认为没有更多数据
					if (newList.length < this.pageSize) {
						this.noMore = true;
					}
				} catch (error) {
					console.error('获取VIP列表失败:', error);
				} finally {
					this.loading = false;
				}
			},
			onFilterChange(e) {
				this.filterIndex = e.detail.value;
				this.searchKeyword = '';
				this.$refs.searchBar.clear(); // 调用 uni-search-bar 的 clear 方法清空输入框
			},
			onSearchConfirm(e) {
				this.searchKeyword = e.value;
				this.pageNum = 1;
				this.vipList = [];
				this.loadMoreVips(this.userInfo.msqStore.id);
			}
		},
		onReachBottom() {
			// 触底加载更多
			this.loadMoreVips(this.userInfo.msqStore.id);
		}
	};
</script>
<style lang="scss" scoped>
	page,
	body {
		height: 100%;
	}

	.searchBox {
		background-color: #fff;
		padding: 20rpx;
		display: flex;
		align-items: center;

		.filterBox {
			margin-right: 20rpx;
		}

		.filterPicker {
			width: 150rpx; // 设置筛选条件选择器的宽度
		}

		.searchInput {
			flex-grow: 1;
		}
	}

	.moneyBox {
		display: flex;
		flex-direction: column;
		padding: 36rpx 43rpx;
		margin: 30rpx;
		border-radius: 20rpx;
		background: #fff;

		.topUser {
			width: 100%;
			display: flex;
			justify-content: space-between;
			align-items: center;
			border-bottom: 2rpx solid #1D85E1;
			padding-bottom: 10rpx;

			.avatar {
				width: 80rpx;
				height: 80rpx;
				border-radius: 50%;
			}

			p {
				margin: 0;
			}
		}

		.bottomMoney {
			display: flex;
			flex-wrap: wrap;
			color: #666666;
			background-color: #F6F6F6;
			padding: 10rpx;

			p {
				width: 50%;
				margin: 10rpx 0rpx;
			}
		}
	}

	.loading {
		text-align: center;
		color: #999;
		padding: 20rpx;
	}

	.no-more {
		text-align: center;
		color: #ccc;
		padding: 20rpx;
	}
</style>